/**
 * Created by guany on 2017/1/8.
 */

var util = {
    /***
     * 将任意数量对象或者字符串传入该方法，若有任意一个为空，则返回true
     * 可判断 空对象 空数组 空字符串 undefined null
     * @returns {boolean}
     */
    isEmpty: function (){
        var len = arguments.length;

        var getType = Object.prototype.toString;

        if(len==0){ return true; }
        for(var i=0;i<len;i++){
            var a = arguments[i];
            if(typeof a === "undefined" || a == null || (typeof a === "string" && a.trim() == "")){
                return true;
            }else if(typeof a === "object"){
                var flg = true;
                for (var name in a ) {
                    flg=false;
                    break;
                }
                if(flg){ return true; }
            }
        }
        return false;
    },
    isInteger:function (obj) {
        return typeof obj === 'number' && obj%1 === 0
    },
    /***
     * 格式化记录方法，传入数字类型的记录数，格式化成中文格式，可以传入单位或者不传
     * @param num
     * @param unit
     * @returns {*}
     */
    formatRecord:function(num,unit){
        var level = num.toString().length/4;
        var sNum = num.toString();
        var arr,result;
        var _unit = Util.isEmpty(unit)?"":" "+unit;
        if(level > 0 && level <= 1){
            result=num+_unit;
        }else if(level>1 && level <= 2){
            result = sNum.substr(0,sNum.length-4)+" 万 "+ parseInt(sNum.substr(sNum.length-4)) + _unit;
        }else if(level>2 && level <= 3){
            result = sNum.substr(0,sNum.length-8)+" 亿 " + parseInt(sNum.substr(sNum.length-8,sNum.length-5)) + " 万 " + _unit;
        }else{
            arr = (num/100000000).toString().split(".")[0];
            result=arr+" 亿" + _unit;
        }
        return result;
    },
    nanoNum:function(num){//999 1000亿2345万6789
        var sNum = num.toString();
        var finalNum,len = sNum.length;
        if(len<=3){
            finalNum = num
        }else if(len == 4){
            finalNum = sNum.substr(0,1) + "千+";
        }else if(len > 4 && len < 8){
            finalNum = sNum.substr(0,len-4) + "万+";
        }else if(len == 8){
            finalNum = sNum.substr(0,1) + "千万+";
        }else if(len > 8 && len < 12){
            finalNum = sNum.substr(0,len-8) + "亿+";
        }else if(len == 12){
            finalNum = sNum.substr(0,1) + "千亿+";
        }else if(len > 12){
            finalNum = "9千亿+";
        }
        return finalNum;
    },
    formatFileSize:function(bytes) {
        if (parseInt(bytes) === 0) return '0B';
        var k = 1024,
            sizes = ['B','KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
            i = Math.floor(Math.log(bytes) / Math.log(k));
        var val = (bytes / Math.pow(k, i)).toFixed(2);
        var to_val = parseInt(val) == val?(val*1).toFixed(0):val;
        return to_val + sizes[i];
    },
    decodeRecord:function(num){
        var num1=0,num2=0,num3= 0;
        var array1 = num.split('亿'),array2 = [];
        if(array1.length>1){
            num1 = parseInt(array1[0]);
            array2 = array1[1].split('万');
        }else{
            num1 = 0;
            array2 = array1[0].split('万');
        }
        if(array2.length>1){
            num2 = parseInt(array2[0]);
            num3 = parseInt(array2[1]);
        }else{
            num2 = 0;
            num3 = parseInt(array2[0]);
        }
        return [num1,num2,num3];
    },
    /* 数据导入时间格式化 */
    exchangeTime: function (mss) {
        var days = parseInt(mss / (1000 * 60 * 60 * 24));
        var hours = parseInt((mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
        var minutes = parseInt((mss % (1000 * 60 * 60)) / (1000 * 60));
        var seconds = parseInt((mss % (1000 * 60)) / 1000);
        var str = '';
        if (days) str = str + days + "天";
        if (hours) str = str + hours + "时";
        if (minutes) str = str + minutes + "分";
        if (seconds) str = str + seconds + "秒";
        return str;
    },
    rand: function (digit) {
        var n="";
        for(var i=0;i<digit;i++){
            n+=Math.floor(Math.random()*10);
        }
        return n;
    },
    extend: function (obj1,obj2) {
        var result = {};
        for(var attr in obj1){
            result[attr] = obj1[attr];
        }
        for(var attr in obj2){
            result[attr] = obj2[attr];
        }
        return result;
    }
};
module.exports = util;